package utils

import (
	"pt-gin-mongo/constant/resCode"
	"pt-gin-mongo/error"

	"github.com/gin-gonic/gin"
)

type responseInfo struct {
	Code int         `json:"code"`
	Msg  interface{} `json:"msg"`
	Data interface{} `json:"data"`
}

func ResponseOK(c *gin.Context, data interface{}) {
	c.JSON(200, responseInfo{
		Code: resCode.OK,
		Msg:  "success",
		Data: data,
	})
}

func ResponseError(c *gin.Context, msg string, data interface{}) {
	c.JSON(200, responseInfo{
		Code: resCode.Error,
		Msg:  msg,
		Data: data,
	})
}

func ResponseTokenBad(c *gin.Context) {
	c.JSON(200, responseInfo{
		Code: resCode.TokenBad,
		Msg:  "token非法或已过期",
	})
}

func ResponseApiError(c *gin.Context, apiError error.ApiError) {
	if apiError.Code == resCode.Error {
		ResponseError(c, apiError.Message, apiError.Data)
	} else if apiError.Code == resCode.TokenBad {
		ResponseTokenBad(c)
	}
}
